package com.itfenghuang;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class A02_查询 {
     /*  针对Student表进行增删改查,
     *      增: 域编译,控制台输入
     *      删: 控制台输入sid编号,删除数据库中对应的数据
     *      改: 控制台输入sid以及要修改的字段值,修改数据库中对应对的数据
     *      查: 控制台输入名字,年龄去查询数据库中的数据,展示在控制台上
      */
     public static void main(String[] args) throws Exception{
         Scanner sc = new Scanner(System.in);
         //1.加载驱动
         Class.forName("com.mysql.jdbc.Driver");
         //2.获取连接
         Connection conn = DriverManager.getConnection
                 ("jdbc:mysql://127.0.0.1:3306/aa_chun_day02", "root", "123456");
         //3.占位符
         String sql="select * from student where sname=? and sage=?";
         //4.获取域编译对象,    占位符赋值,调用set方法赋值
         PreparedStatement pap = conn.prepareStatement(sql);
         pap.setString(1,sc.nextLine());
         pap.setInt(2,sc.nextInt());
         sc.nextLine();
         //5.执行sql语句
         ResultSet resultSet = pap.executeQuery();
         while (resultSet.next()){
             String sname = resultSet.getString("sname");
             int sage = resultSet.getInt("sage");
             String course = resultSet.getString("course");
             System.out.println(sname+" "+sage+" "+course);
         }
         resultSet.close();
         pap.close();
         conn.close();
     }

}
